package com.chuangjiangx.member.basic.web.controller;

import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.basic.dao.model.InMbrConfig;
import com.chuangjiangx.member.basic.web.response.MerchantInfoResponse;
import com.chuangjiangx.member.coupon.ddd.query.MbrCardQuery;
import com.chuangjiangx.member.basic.ddd.query.MbrConfigQuery;
import com.chuangjiangx.member.basic.ddd.query.MerchantInfoQuery;
import com.chuangjiangx.member.basic.ddd.query.dto.MerchantInfoDTO;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrUserContext;
import com.chuangjiangx.publicmodule.domain.upload.service.UploadFileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 查询商户信息接口
 * Created by YangKuo on 2017/11/22.
 */
@RestController
@RequestMapping(value = "/h5", produces = "application/json")
public class MerchantController extends BaseController {

    @Autowired
    private MerchantInfoQuery merchantInfoQuery;
    @Autowired
    private UploadFileService uploadFileService;
    @Autowired
    private MbrCardQuery mbrCardQuery;
    @Autowired
    private MbrConfigQuery mbrConfigQuery;

    /**
     * 根据商户Id查询商户信息
     * 兼容登录和不登录两种方式
     * @param request
     * @return com.chuangjiangx.commons.response.UnderlineResponse
     * @see MerchantInfoResponse
     */
    @RequestMapping(value = "/search-merchant-info")
    @ResponseBody
    public Response searchMerchantInfo(HttpServletRequest request, @RequestParam(defaultValue = "0") Long merchantId) {
        Long mid = null;
        MbrUserContext user = loginService.currentUserContext();
        if(0 != merchantId){
            mid = merchantId;
        }else{
            if(user == null || null == user.getMerchantId()){
                return ResponseUtils.error("","无法获取商户信息");
            }
            mid = user.getMerchantId();
        }

        MerchantInfoResponse response = new MerchantInfoResponse();
        if(mid != null){
            MerchantInfoDTO dto = merchantInfoQuery.searchMerchantInfoForH5(mid);
            String logoUrl = uploadFileService.getDownloadUrl(dto.getLogoUrl());
            response
                    .setMerchantId(mid)
                    .setName(dto.getName())
                    .setLogoUrl(logoUrl);

            //设置商户储值功能状态
            InMbrConfig inMbrConfig = mbrConfigQuery.queryByMerchantId(mid);
            if(inMbrConfig != null){
                // 设置是否可以使用储值功能
                response.setIsCanUseStoredCard(inMbrConfig.getStoredFunc());
                // 封面选择 1:背景色 2：背景图
                Integer cardCoverChoice = inMbrConfig.getCardCoverChoice();
                if(cardCoverChoice != null){
                    if(2 == cardCoverChoice){
                        // 会员卡背景图
                        String cardPicture = inMbrConfig.getCardPicture();
                        if(StringUtils.isNotBlank(cardPicture)){
                            // 判断是否有设置 会员卡背景图
                            response.setCardPicture(cardPicture);
                        }
                    }else if (1 == cardCoverChoice){
                        // 会员卡背景色
                        // 目前用默认的了就好了
                    }
                }
            }
        }


        // 查询是否有卡券营销菜单
        Integer count = merchantInfoQuery.countMerchantComponent(695L);
        response.setIsCanUseCoupon(count < 1 ? 0 : 1);

        return ResponseUtils.success(response);
    }

}
